package com.example.starapp.parnterfrag;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import com.example.starapp.R;
import com.example.starapp.starfrag.StarInfo;
import com.example.starapp.utils.AssetsUtils;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ParnterFragment extends Fragment implements View.OnClickListener , AdapterView.OnItemSelectedListener {
        ImageView manIv,womanIv;
        Spinner manSp,womanSp;
        Button prizeBtn,analysisBtn;

private List<StarInfo.StarinfoBean> infoList;  // 上一级界面传来的数据
        List<String>nameList;   //存放星座名词的集合
private Map<String, Bitmap> contentlogoImgMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parnter, container, false);
        initView(view);
//        获取activity传来的数据
        Bundle bundle = getArguments();
        StarInfo info = (StarInfo) bundle.getSerializable("info");
        infoList = info.getStarinfo();
        nameList = new ArrayList<>();
//        获取适配器所需要的数据源
        for (int i = 0; i <infoList.size() ; i++) {
        nameList.add(infoList.get(i).getName());
        }
//        创建适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.item_sp, R.id.item_sp_tv, nameList);
//        设置适配器
        manSp.setAdapter(adapter);
        womanSp.setAdapter(adapter);

//       获取第一个图片显示在imageview上
        String logoname = infoList.get(0).getLogoname();
        contentlogoImgMap = AssetsUtils.getContentLogoImgMap();
        Bitmap bm = contentlogoImgMap.get(logoname);
        manIv.setImageBitmap(bm);
        womanIv.setImageBitmap(bm);


        return view;
        }
/* 初始化布局当中所有用到的控件，并且进行监听*/
private void initView(View view) {
        manIv = view.findViewById(R.id.parnterfrag_iv_man);
        womanIv = view.findViewById(R.id.parnterfrag_iv_woman);
        manSp = view.findViewById(R.id.parnterfrag_sp_man);
        womanSp = view.findViewById(R.id.parnterfrag_sp_woman);
        prizeBtn = view.findViewById(R.id.parnterfrag_btn_prize);
        analysisBtn = view.findViewById(R.id.parnterfrag_btn_analysis);
        //监听button的点击事件
        prizeBtn.setOnClickListener(this);
        analysisBtn.setOnClickListener(this);
        //监听spinner被选中item事件
        manSp.setOnItemSelectedListener(this);
        womanSp.setOnItemSelectedListener(this);
        }

@Override
public void onClick(View v) {
        switch (v.getId()) {
        case R.id.parnterfrag_btn_analysis:  // 星座分析
//                获取选择的星座位置
        int manselPos = manSp.getSelectedItemPosition();
        int womanselPos = womanSp.getSelectedItemPosition();
//                传值，跳转到分析界面
        Intent intent = new Intent(getContext(), ParnterAnalysisActivity.class);
        intent.putExtra("man_name",infoList.get(manselPos).getName());
        intent.putExtra("man_logo",infoList.get(manselPos).getLogoname());
        intent.putExtra("woman_name",infoList.get(womanselPos).getName());
        intent.putExtra("woman_logo",infoList.get(womanselPos).getLogoname());
        startActivity(intent);
        break;
        case R.id.parnterfrag_btn_prize:  //开始抽奖

        break;
        }
        }

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
        case R.id.parnterfrag_sp_man:
        String logoname = infoList.get(position).getLogoname();
        Bitmap bm = contentlogoImgMap.get(logoname);
        manIv.setImageBitmap(bm);

        break;
        case R.id.parnterfrag_sp_woman:
        logoname = infoList.get(position).getLogoname();
        bm = contentlogoImgMap.get(logoname);
        womanIv.setImageBitmap(bm);
        break;
        }
        }

@Override
public void onNothingSelected(AdapterView<?> parent) {

        }
        }
